﻿using System;
using System.Web;
using Newtonsoft.Json;
using eCommerce.core;
using eCommerce.core.Helpers;
using eCommerce.db.BORespository;
using eCommerce.db.DataRespository;
using eCommerce.log;

namespace eCommerce.db.Action
{
    public class ArticleAction : IBaseAction
    {
        public void Do(HttpContext context)
        {
            var actionName = Utils.GetIdFromRequestString(Utils.Action, string.Empty);

            var responseData = new ResponseData();
            switch (actionName)
            {
                case "search":
                    responseData = ListSearch();
                    break;                

                case "changestatus":
                    responseData = ChangeStatus();
                    break;
                case "delete":
                    responseData = Delete();
                    break;

                case "":
                    responseData.Success = false;
                    responseData.Message = "Can not found this action.";
                    break;
            }

            var jSonString = JsonConvert.SerializeObject(responseData, Utils.SerializeFormat("MM/dd/yyyy HH:mm:ss"));
            if (!string.IsNullOrEmpty(context.Request.Params["callback"]))
                jSonString = string.Format("{0}({1})", context.Request.Params["callback"], jSonString);
            context.Response.Write(jSonString);
        }

        private static ResponseData ListSearch()
        {
            var responseData = new ResponseData();
            
            var keyword     = Utils.GetIdFromRequestString(Utils.Keyword, string.Empty);
            var categoryId  = Utils.GetIdFromRequestString(Utils.CategoryId, 0);
            var status      = Utils.GetIdFromRequestString(Utils.Status, 0);
            var orderBy     = Utils.GetIdFromRequestString(Utils.OrderBy, 0);
            var pageIndex   = Utils.GetIdFromRequestString(Utils.PageIndex, 0);
            var pageSize    = Utils.GetIdFromRequestString(Utils.PageSize, 0);

            try
            {
                var totalRecords = 0;
                var lstArticle = ArticleBo.Search(keyword, categoryId, status, orderBy, pageIndex, pageSize, ref totalRecords);

                foreach (var article in lstArticle)
                {
                    article.FormatPublishOn = Utils.FormatIn_HHMM_DDMMYYYY(article.PublishOn);
                    article.CategoryName    = CategoryBo.GetCategoryName(article.CategoryId);

                    switch (article.Status)
                    {
                        case (int) ArticleStatus.Temp:
                            article.IconStatus = "<a class='btn14 topDir' title='Chờ xuất bản'><img src='/images/icons/dark/denied.png' alt=''/></a>";
                            break;
                        case (int)ArticleStatus.Published:
                            article.IconStatus = "<a class='btn14 topDir' title='Đã xuất bản'><img src='/images/icons/dark/clock.png' alt=''/></a>";
                            break;
                        case (int)ArticleStatus.Focus:
                            article.IconStatus = "<a class='btn14 topDir' title='Bài viết nổi bật'><img src='/images/icons/dark/paperclip.png' alt=''/></a>";
                            break;
                    }
                }

                responseData.Success  = true;
                responseData.Data     = lstArticle;
                responseData.TotalRow = totalRecords;
                responseData.Pager    = PagerHelper.PageLinks(pageIndex, pageSize, totalRecords, Constant.PageStep, UrlHelper.PagerArticle);
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }

            return responseData;
        }

        private static ResponseData ChangeStatus()
        {
            var responseData = new ResponseData();

            var username = Utils.GetIdFromRequestString(Utils.UserName, 0);
            var id       = Utils.GetIdFromRequestString(Utils.ArticleId, 0);
            var status   = Utils.GetIdFromRequestString(Utils.Status, 0);

            try
            {
                ArticleBo.ChangeStatus(username, id, status);

                responseData.Success  = true;
                responseData.Data     = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }        

        private static ResponseData Delete()
        {
            var responseData = new ResponseData();

            var id = Utils.GetIdFromRequestString(Utils.ArticleId, 0);

            try
            {
                ArticleBo.Delete(id);

                responseData.Success = true;
                responseData.Data = null;
                responseData.TotalRow = 0;
            }
            catch (Exception ex)
            {
                responseData.Success = false;
                responseData.Message = ex.Message;
                Logger.WriteLog(Logger.LogType.Error, ex.Message);
            }
            return responseData;
        }
    }
}
